const app = getApp()
const util = require('../../../utils/util')
Page({
	data: {
		id:'',
		userid:'',
		indexData:[],
		baseUrl: app.globalData.baseUrl,
		jiedianBox:[],
		jinduBox:[],
		list:[{zhankai:false},{zhankai:false},{zhankai:false},{zhankai:false},{zhankai:false}],
		changeon:false,
		show: false,
		timeItem:'',
		timeId:'',
		ryList:[],
		pid:'',
		thisRy:[],
		newJindu:[],
		xinSHow:false,
		formDate:{
			biddingTime:'',
			bidTime:'',
			visit:'',
			communicate:'',
			needSupport:'',
			statusSummary:'',
			progress:'',
			projectId:''
		}
	},
	onLoad(options) {
		this.data.formDate.projectId=options.id
		this.setData({
			id:options.id,
			userid:wx.getStorageSync('user').id,
      nickname:wx.getStorageSync('user').nickname,
      formDate:this.data.formDate
		})
		var list=[]
		wx.getStorageSync('ryList').forEach(a => {
			if(a.position==6){
				list.push({nickname:a.nickname,id:a.id})
			}
		});
		this.setData({
			ryList:list
		})
		this.ready()
	},
	ready(){
		var _this=this
		app.indexApi.productView(this.data.id).then(a => {
			if (a.code == 0) {
				wx.getStorageSync('ryList').forEach(item => {
					if(a.data.createBy==item.id){
						a.data.createBy=item.nickname
						a.data.head=item.avatar
					}
				});
				
				_this.data.ryList.forEach(item => {
					if(item.id==a.data.industryManager){
						a.data.industryManager=item.nickname
						_this.setData({
							indexData:a.data
						})
					}
				});
			}
		})
		app.indexApi.jiedianhq(this.data.id).then(a => {
			if (a.code == 0) {
				var visit=[]
				var communicate=[]
				var needSupport=[]
				var statusSummary=[]
				var progress=[]
				a.data.forEach(item => {
					visit.push({id:item.id,con:item.visit,date:item.createTime.slice(5,10)})
					communicate.push({id:item.id,con:item.communicate,date:item.createTime.slice(5,10)})
					needSupport.push({id:item.id,con:item.needSupport})
					statusSummary.push({id:item.id,con:item.statusSummary})
					progress.push({date:item.createTime.slice(5,10),con:item.progress,id:item.id})
				});
				const removeDuplicateObj = (arr) => {
					let obj = {};
					arr = arr.reduce((newArr, next) => {
					  obj[next.con] ? "" : (obj[next.con] = true && newArr.push(next));
					  return newArr;
					}, []);
					return arr;
				  };
				a.data[0].visit=removeDuplicateObj(visit)
				a.data[0].communicate=removeDuplicateObj(communicate)
				a.data[0].needSupport=removeDuplicateObj(needSupport)
				a.data[0].statusSummary=removeDuplicateObj(statusSummary)
				a.data[0].progress=progress
				this.setData({
					jiedianBox:a.data
				})
			
			}
		})
		app.indexApi.shenpijidu(this.data.id).then(a => {
			if (a.code == 0) {
				a.data.forEach(item => {
					wx.getStorageSync('ryList').forEach(b => {
						if(item.userId==b.id){
							item.userNickname=b.nickname
							item.head_img=b.avatar
							var positionList=[{id:1,name:'总经办'},{id:2,name:'州三级经理'},{id:3,name:'县市三级经理'},{id:4,name:'县市主管'},{id:5,name:'客户经理'},{id:6,name:'行业经理'}]
							positionList.forEach(c => {
							if(b.position==c.id){
								item.position_name=c.name
							}
						});
						}
					});
				});
				a.data[0].approvalTime=this.data.indexData.createTime
				this.setData({
					jinduBox:a.data
				})
			}
		})
	},
	shenhe(e){
		let type = e.target.dataset.type;
		var str=[]
			this.data.jinduBox.forEach(a => {
				if(this.data.indexData.auditProcess==a.schedule){
					str.push(a)
				}
		});
		if(type==1){
			str[0].state=1
			str[0].rejection=null
			app.indexApi.bohui(str[0]).then(a => {
				if (a.code == 0) {
					this.ready()
				}
			})
		}else{
			wx.showModal({
				editable:true,//显示输入框
				placeholderText:'请输入驳回原因',//显示输入框提示信息
				success: res => {
					if (res.confirm) { //点击了确认
						str[0].state=2
						str[0].rejection=res.content
						app.indexApi.bohui(str[0]).then(a => {
							if (a.code == 0) {
								this.ready()
							}
						})
				   } 
				}
			})
		}
		

	},
	shanchu(){
		app.indexApi.shanchu(this.data.id).then(a => {
			if (a.code == 0) {
				wx.showToast({
					title: '删除成功', //提示的内容
					icon: 'none', //图标有success、error、loading、none四种
				 })
				 setTimeout(function () {
					wx.navigateBack({
						delta: -1,
						fail:function(){
						  wx.navigateTo({
							url: "/pages/index/index"
						  })
						}
					})
				 },1000)
			}
		})
	},
	chehui(){
		app.indexApi.chehui(this.data.id).then(a => {
			if (a.code == 0) {
				wx.showToast({
					title: '撤回成功', //提示的内容
					icon: 'none', //图标有success、error、loading、none四种
				 })
				this.ready()
			}
		})
	},
	againsubmit(){
		app.indexApi.againSubmit(this.data.id).then(a => {
			if (a.code == 0) {
				wx.showToast({
					title: '提交成功', //提示的内容
					icon: 'none', //图标有success、error、loading、none四种
				 })
				this.ready()
			}
		})
	},
	changeShow(){
		this.setData({
			xinSHow:true
		})
	},
	changeCon(){
    this.setData({
      xinSHow:false
    })
	},
	bindinput(e) {
		let item = e.target.dataset.item;
		const array = this.data.formDate;
		array[item] = e.detail.value;
		this.setData({ formDate: array });
	},
	isNos(){
		app.indexApi.productUpdate(this.data.formDate).then(a => {
			if (a.code == 0) {
				wx.showToast({
					title: '修改成功', //提示的内容
					icon: 'none', //图标有success、error、loading、none四种
				 })
				 this.setData({
					xinSHow:false
				 })
				this.ready()
			}
		})
	},
	changeBtn(e){
		var _this=this
		var item=e.currentTarget.dataset.item
		var id=e.currentTarget.dataset.id
		if(this.data.changeon==true){
			wx.showModal({
				editable:true,//显示输入框
				placeholderText:'修改节点',//显示输入框提示信息
				success: res => {
					if (res.confirm) { //点击了确认
					var str={
						[item]:res.content,
						id:id,
						projectId:_this.data.id
					}
					app.indexApi.productChange(str).then(a => {
						if (a.code == 0) {
							wx.showToast({
								title: '修改成功', //提示的内容
								icon: 'none', //图标有success、error、loading、none四种
							 })
							this.ready()
						}
					})
				   } 
				}
			})
		}	
	},
	prochangeBtn(e){
		var _this=this
		var item=e.currentTarget.dataset.item
		if(this.data.changeon==true){
			wx.showModal({
				editable:true,//显示输入框
				placeholderText:'修改节点',//显示输入框提示信息
				success: res => {
					if (res.confirm) { //点击了确认
					var str={
						[item]:res.content,
						accountManager:_this.data.indexData.accountManager,
						id:_this.data.indexData.id
					}
					app.indexApi.productChangeMax(str).then(a => {
						if (a.code == 0) {
							wx.showToast({
								title: '修改成功', //提示的内容
								icon: 'none', //图标有success、error、loading、none四种
							 })
							this.ready()
						}
					})
				   } 
				}
			})
		}	
	},
	selectType(e) {
		let key = e.detail.value;
		var id=this.data.ryList[key].id
		var str={
			industryManager:id,
			accountManager:this.data.indexData.accountManager,
			id:this.data.indexData.id
		}
		app.indexApi.productChangeMax(str).then(a => {
			if (a.code == 0) {
				wx.showToast({
					title: '修改成功', //提示的内容
					icon: 'none', //图标有success、error、loading、none四种
				 })
				this.ready()
			}
		})
		
	  },
	selchangeBtn(e){
		var _this=this
		var item=e.currentTarget.dataset.item
		var id=e.currentTarget.dataset.id
		if(this.data.changeon==true){
			wx.showActionSheet({
				itemList:['否','是'], //文字数组
				success: (res) => {
					var str={
						[item]:res.tapIndex,
						id:id,
						projectId:_this.data.id
					}
					app.indexApi.productChange(str).then(a => {
						if (a.code == 0) {
							wx.showToast({
								title: '修改成功', //提示的内容
								icon: 'none', //图标有success、error、loading、none四种
							 })
							this.ready()
						}
					})
				}
			  })
		}	
	},
	changeA(){
		this.setData({
			show: false
		})  
	  },
	timechangeBtn (e) {
		let item = e.target.dataset.item;
		var id=e.target.dataset.id
		this.setData({
		  show: true,
		  timeItem:item,
		  timeId:id
		})
	},
	handleChange (e) {
		// 选择器所抛出的结果
		const timestamp = e.detail.value
		var str={
			[this.data.timeItem]:util.formatTime(timestamp, '{y}-{m}-{d} {h}:{i}'),
			id:this.data.timeId,
			projectId:this.data.id
		}
		app.indexApi.productChange(str).then(a => {
			if (a.code == 0) {
				wx.showToast({
					title: '修改成功', //提示的内容
					icon: 'none', //图标有success、error、loading、none四种
				 })
				 this.setData({
					show: false
				})
				this.ready()
			}
		})
	},
	changeActive(){
		var active=!this.data.changeon
		this.setData({
			changeon:active
		})
	},
	zhankai:function(e){
        let zk = e.currentTarget.dataset.index
        let list=this.data.list;
        list[zk].zhankai = !list[zk].zhankai || false;
        this.setData({
            list
        })
	},
	shouqi:function(e){
        let zk = e.currentTarget.dataset.index
        let list=this.data.list;
        list[zk].zhankai = false || !list[zk].zhankai;
        this.setData({
            list
        })
    },
	back(){
		wx.navigateBack({
			delta: -2,
			fail:function(){
			  wx.navigateTo({
				url: "/pages/index/index"
			  })
			}
		})
	}
})